﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine;
using SharpDX;
using SharpDX.D3DCompiler;
using SharpDX.Direct3D11;
using SharpDX.Direct3D;
using SharpDX.DXGI;
using SharpDX.Windows;
using SharpDX.DirectInput;
using SharpDX.Multimedia;
using SharpDX.RawInput;
using Device = SharpDX.Direct3D11.Device;
using Resource = SharpDX.Direct3D11.Resource;
using System.Windows.Forms;
using ModelViewer;

namespace Engine
{
    public static class FPSCamera
    {
        public static float Speed = 40f;

        public static float yawn = 3.14f / 2f;
        public static float pitch = 0f;

        static bool PreKeyEnter = false;

        public static void Update(float TimeMili, EEDCamera Cam)
        {
            if (InputManager.KeyPressed(Keys.W))
                Cam.position += Conversion.ToDoubleVector(Cam.direction) * TimeMili * Speed * 0.001f;
            if (InputManager.KeyPressed(Keys.S))
                Cam.position -= Conversion.ToDoubleVector(Cam.direction) * TimeMili * Speed * 0.001f;
            if (InputManager.KeyPressed(Keys.D))
            {
                Vector3 R = Vector3.Cross(Cam.direction, new Vector3(0, 1, 0));
                Cam.position += Conversion.ToDoubleVector(R) * TimeMili * Speed * 0.001f;
            }
            if (InputManager.KeyPressed(Keys.A))
            {
                Vector3 R = Vector3.Cross(Cam.direction, new Vector3(0, 1, 0));
                Cam.position -= Conversion.ToDoubleVector(R) * TimeMili * Speed * 0.001f;
            }
            if (InputManager.KeyPressed(Keys.Space))
                Cam.position += Conversion.ToDoubleVector(new Vector3(0, 1, 0)) * TimeMili * Speed * 0.001f;
            if (InputManager.KeyPressed(Keys.ControlKey))
                Cam.position -= Conversion.ToDoubleVector(new Vector3(0, 1, 0)) * TimeMili * Speed * 0.001f;

            if (InputManager.KeyPressed(Keys.D1))
                Speed = 10f;
            if (InputManager.KeyPressed(Keys.D2))
                Speed = 20f;
            if (InputManager.KeyPressed(Keys.D3))
                Speed = 40f;
            if (InputManager.KeyPressed(Keys.D4))
                Speed = 80f;
            if (InputManager.KeyPressed(Keys.D5))
                Speed = 160f;
            if (InputManager.KeyPressed(Keys.D6))
                Speed = 320f;
            if (InputManager.KeyPressed(Keys.D7))
                Speed = 640;

            if (InputManager.PreRightMouse && !InputManager.RightMouse)
            {
                InputManager.CursorToCenter = !InputManager.CursorToCenter;
            }

            if (InputManager.CursorToCenter)
            {
                yawn -= InputManager.MouseDelta.X / 1000f;
                if (yawn < -2d*Math.PI) yawn = 0;//-(float)Math.PI;
                if (yawn > 2d*Math.PI) yawn = 0;//(float)Math.PI;

                pitch -= InputManager.MouseDelta.Y / 1000f;
                if (pitch <= -Math.PI / 2d) pitch = -(float)Math.PI / 2f;//-(float)Math.PI;
                if (pitch > Math.PI / 2d) pitch = (float)Math.PI / 2f;//(float)Math.PI;
            }

            Quaternion quat = Quaternion.RotationYawPitchRoll((float)yawn, (float)pitch, 0);
            Cam.quaternion = quat;

            //Program.form.Text = Cam.position.ToString();

            if (PreKeyEnter && !InputManager.KeyPressed(Keys.Enter))
            {
                //Shapshot
                DebugDraw.ShadowsShapshot();
            }
            PreKeyEnter = InputManager.KeyPressed(Keys.Enter);
        }

        
    }
}
